package cn.ruanyun.backInterface.modules.fadada.controller;

import cn.ruanyun.backInterface.common.utils.PageUtil;
import cn.ruanyun.backInterface.common.utils.ResultUtil;
import cn.ruanyun.backInterface.common.vo.PageVo;
import cn.ruanyun.backInterface.common.vo.Result;
import cn.ruanyun.backInterface.modules.fadada.pojo.MyContractSigning;
import cn.ruanyun.backInterface.modules.fadada.pojo.MyContractSigningCheck;
import cn.ruanyun.backInterface.modules.fadada.service.IMyContractSigningCheckService;
import com.google.api.client.util.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Optional;

/**
 * @author z
 * 我的合同签署审核管理接口
 */
@Slf4j
@RestController
@RequestMapping("/ruanyun/myContractSigningCheck")
@Transactional
@Api(tags = "合同审核")
public class MyContractSigningCheckController {

    @Autowired
    private IMyContractSigningCheckService iMyContractSigningCheckService;


    @PostMapping("/getMyContractDetail")
    @ApiOperation("获取我的审核合同最新的一个")
    public Result<MyContractSigningCheck> getMyContractDetail() {

        return iMyContractSigningCheckService.getMyContractDetail();
    }


    @PostMapping("/checkMyContractSigningCheck")
    @ApiOperation("后台审核法大大")
    public Result<Object> checkMyContractSigningCheck(@Validated @NotBlank(message = "code不能为空") Integer code, String checkReason, @Validated @NotBlank(message = "id不能为空！") String id) {

        return iMyContractSigningCheckService.checkMyContractSigningCheck(code, checkReason,  id);
    }


    @PostMapping("/contractFilling")
    @ApiOperation("合同归档")
    public Result<Object> contractFilling(String id) {

        return iMyContractSigningCheckService.contractFilling(id);
    }

    @PostMapping("/fadadaOneNotify")
    @ApiOperation("法大大甲方签署回调")
    public String fadadaOneNotify(HttpServletRequest request) throws Throwable {

        return iMyContractSigningCheckService.fadadaOneNotify(request);
    }

    @PostMapping("/fadadaPartyTwoNotify")
    @ApiOperation("法大大乙方签署回调")
    public String fadadaPartyTwoNotify(HttpServletRequest request) throws Throwable {

        return iMyContractSigningCheckService.fadadaPartyTwoNotify(request);
    }

    @PostMapping("/pcGetmyContractSigning")
    @ApiOperation("后台获取合同审核列表")
    public Result<Object> pcGetmyContractSigning(MyContractSigning myContractSigning, PageVo pageVo) {

        return Optional.ofNullable(iMyContractSigningCheckService.pcGetmyContractSigning(myContractSigning))
                .map(pcGetmyContractSigning-> {
                    Map<String,Object> result = Maps.newHashMap();
                    result.put("size",pcGetmyContractSigning.size());
                    result.put("data", PageUtil.listToPage(pageVo,pcGetmyContractSigning));
                    return new ResultUtil<>().setData(result,"后端获取合同审核列表成功！");
                })
                .orElse(new ResultUtil<>().setErrorMsg(201,"暂无数据！"));

    }

}
